package com.fysports.web.kit.sms;

import com.fysports.web.kit.commons.DateTimeKit;

import java.io.Serializable;
import java.util.Date;


/**
 * 存放验证码
 * Created by konbluesky
 * Date : 15/7/3 下午5:36
 * Project :jdmdata_server
 */
public class VerifyCode implements Serializable {
    /*验证码*/
    private Double code;
    /*过期时间*/
    private long pasttime;

    private boolean isvalid;


    public VerifyCode() {
        /*默认过期时间当前时间后五分钟*/
        pasttime = new Date().getTime() + DateTimeKit.time_minutes * 5 * 1000;
        /*生成验证码*/
        code = Math.random() * 9000 + 1000;
        isvalid = true;
    }


    /**
     * 过期时间
     *
     * @param ptime
     */
    public VerifyCode(Date ptime) {
        pasttime = ptime.getTime();
        /*生成验证码*/
        code = Math.random() * 9000 + 1000;
        isvalid = true;
    }


    public Double getCode() {
        return code;
    }


    public void setCode(Double code) {
        this.code = code;
    }


    public long getPasttime() {
        return pasttime;
    }


    public void setPasttime(long pasttime) {
        this.pasttime = pasttime;
    }


    /**
     * 判断验证码是否有效,只能验证一次
     *
     * @param code
     * @return
     */
    public boolean equals(String code) {
        boolean status = false;
        /*如果过期*/
        if (this.getPasttime() < new Date().getTime()) {
            setIsvalid(false);
        }
        if (isvalid() && this.getCode().intValue() == Integer.parseInt(code)) {
            status = true;
        }

        return status;
    }


    /**
     * 是否有效
     *
     * @return
     */
    public boolean isvalid() {
        return isvalid;
    }


    public void setIsvalid(boolean isvalid) {
        this.isvalid = isvalid;
    }


    public static void main(String[] args) {
        VerifyCode vc = new VerifyCode();
        System.out.println(vc.getCode().intValue());
        System.out.println(new Date(vc.getPasttime()));
    }
}
